package com.bridgeintelligent.tag.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateHelper {

    public static final String PATTERN_DATE_1 = "yyyy-MM-dd";
    public static final String PATTERN_DATE_2 = "HH:mm:ss";
    public static final String PATTERN_DATE_3 = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_DATE_4 = "yyyy-MM";
    public static final String PATTERN_DATE_5 = "yyyyMM";
    public static final String PATTERN_DATE_6 = "yyyy";
    public static final String PATTERN_DATE_7 = "yyyyMMddHHmmss";
    public static final String PATTERN_DATE_8 = "yyyyMMdd";
    public static final String PATTERN_DATE_9 = "HHmmss";
    public static final String PATTERN_DATE_10 = "MM";
    public static final String PATTERN_DATE_11 = "yyMMdd";



    /**
     * 获取当前时间
     *
     * @return
     */
    public static String currentDateTime() {
        return DateFormatUtils.format(new Date(), PATTERN_DATE_3);
    }

    /**
     * 根据pattern获取当前时间
     *
     * @param pattern
     * @return
     */
    public static String currentDateTime(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }


    /**
     * 获取上月底的日期
     * @return
     */
    public static String lastMonth(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_DATE_5);
        LocalDate date = LocalDate.now();
        date = date.minusMonths(1);
        return date.format(formatter);

    }

    /**
     * 获取上一年月底日期
     * @return
     */
    public static String lastYear(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_DATE_6);
        LocalDate date = LocalDate.now();
        date = date.minusYears(1);
        return date.format(formatter)+"12";
    }

    /**
     * 获取上季末月份
     * @return
     */
    public static String lastQuarter()  {
        DateTimeFormatter format1 = DateTimeFormatter.ofPattern(PATTERN_DATE_10);
        DateTimeFormatter format2 = DateTimeFormatter.ofPattern(PATTERN_DATE_6);
        LocalDate date = LocalDate.now();
        String month = date.format(format1);
        String year = date.format(format2);
        if(month.equals("01") || month.equals("02") || month.equals("03")){
            date = date.minusYears(1);
            return date.format(format2)+"12";
        } else if(month.equals("04") || month.equals("05") || month.equals("06")){
            return year+"03";
        }else if(month.equals("07") || month.equals("08") || month.equals("09")){
            return year+"06";
        }else if (month.equals("10") || month.equals("11") || month.equals("12")){
            return year+"09";
        }
        return "";
    }

    public static String beforeYear(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_DATE_6);
        LocalDate date = LocalDate.now();
        date = date.minusYears(1);
        return date.format(formatter);
    }



    public static void main(String[] args)  {
        System.out.println(DateHelper.lastMonth());
        System.out.println(DateHelper.lastYear());
        System.out.println(DateHelper.lastQuarter());
        System.out.println(DateHelper.beforeYear());
    }






}
